from flask import Flask, request, jsonify, abort

app = Flask(__name__)

tasks = [{
    'id': 1,
    'title': u'Buy groceries',
    'description': u'Milk,cheese,Pizza,Fruit,Tylenol',
    'done': False
}, {
    'id': 2,
    'title': u'Learn Python',
    'description': u'Need to find a good Python tutorial on the web',
    'done': False
}]


@app.route('/hello/tasks', methods=['POST'])
def create_task():
    if not request.json or 'title' not in request.json:
        abort(400)
    task = {
        'id': tasks[-1]['id'] + 1,
        'title': request.json['title'],
        'description': request.json.get('description', ""),
        'done': False
    }
    tasks.append(task)
    return jsonify({'task': task}), 201


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5001, debug=True)

# test cmd
# curl -i -H "Content-Type: application/json" -X POST -d '{"title":"Read a book"}' http://localhost:5001/hello/tasks